#include "Date.h"
#include <iostream>
#include <ctime>
#include <cassert>
using namespace std;

Date::Date()
{
    time_t ut = time(NULL);
    tm *tp = localtime(&ut);
    year = tp->tm_year + 1900;
    month = tp->tm_mon + 1;
    day = tp->tm_mday;
    hour = tp->tm_hour;
    minute = tp->tm_min;
    second = tp->tm_sec;
}

Date::Date(int y, int mo, int d, int h, int mi, int s)
{
    assert(y >= 0);
    year = y;
    assert(mo >= 1 && mo <= 12);
    month = mo;
    assert(d >= 1 && d <= 31);
    day = d;
    assert(h >= 0 && h <= 23);
    hour = h;
    assert(mi >= 0 && mi <= 59);
    minute = mi;
    assert(s >= 0 && s <= 59);
    second = s;
}

void Date::showDate()
{
    cout << year << "/" << month << "/" << day;
}

void Date::showTime()
{
    cout << hour << ":" << minute << ":" << second;
}

int Date::operator-(Date &d)
{
    tm t;

    t.tm_year = year-1900;
    t.tm_mon = month-1;
    t.tm_mday = day;
    t.tm_hour = hour;
    t.tm_min = minute;
    t.tm_sec = second;
    t.tm_wday = 0;
    t.tm_yday = 0;
    t.tm_isdst = 0;
    time_t d1 = mktime(&t);

    t.tm_year = d.year-1900;
    t.tm_mon = d.month-1;
    t.tm_mday = d.day;
    t.tm_hour = d.hour;
    t.tm_min = d.minute;
    t.tm_sec = d.second;
    t.tm_wday = 0;
    t.tm_yday = 0;
    t.tm_isdst = 0;
    time_t d2 = mktime(&t);
    int sec = d2 - d1;
    sec = sec > 0 ? sec : -sec;
    return sec / (24*3600);
}
